package com.tarena.elts.ui;

import java.awt.HeadlessException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.tarena.elts.entity.ExamInfo;
import com.tarena.elts.entity.QuestionInfo;
import com.tarena.elts.entity.User;
import com.tarena.elts.service.ExamService;
import com.tarena.elts.service.IdOrPwdException;

/**
 * 登录流程控制
 *
 */
public class ClientContext {
	private Timer timer;
	private QuestionInfo currentQuestionInfo;
	private ExamInfo examInfo;
	private LoginFrame loginFrame;
	/**login()方法信赖的登录窗口对象，注入给引用loginFrame,使loginFrame变量的值不再为null:信赖注入
	 * */
	public void setLoginFrame(LoginFrame loginFrame){
		this.loginFrame = loginFrame;
	}
	private WelcomeWindows welcomeWindows;
	public void setWelcomeWindows(WelcomeWindows welcomeWindows) {
		this.welcomeWindows = welcomeWindows;
	}
	private MenuFrame menuFrame;
	public void setMenuFrame(MenuFrame menuFrame){
		this.menuFrame=menuFrame;
	}
	private ExamService examService;
	public void setExamService(ExamService examService){
		this.examService = examService;
	}
	private ExamFrame examFrame;
	public void setExamFrame(ExamFrame examFrame) {
		this.examFrame = examFrame;
	}
	public void login(){
		try {
			//login()流程信赖登录界面对象，这个对象被LoginFrame变量引用，如果loginFrame变量为null,会发生空指针异常
			int id = loginFrame.getUserId();
			String pwd = loginFrame.getPwd();
			User loginUser = examService.login(id,pwd);
			menuFrame.updateView(loginUser);
			loginFrame.setVisible(false);
			menuFrame.setVisible(true);
		} catch (IdOrPwdException e) {
			// TODO: handle exception
			e.printStackTrace();
			loginFrame.showMessage(e.getMessage());
		}catch(NumberFormatException e){
			e.printStackTrace();
			loginFrame.showMessage("编号必须是数");
		}catch(Exception e){
			e.printStackTrace();
			loginFrame.showMessage(e.getMessage());
		}
	}
	public void showUI(){
		final Timer timer = new Timer();
		welcomeWindows.setVisible(true);
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				welcomeWindows.setVisible(false);
				loginFrame.setVisible(true);
			}
		}, 2000);
	}
	public void exit(JFrame form){
		int val=JOptionPane.showConfirmDialog(form, "exit");
		if(val==JOptionPane.YES_OPTION){
			System.exit(0);
		}
	}
	/**
	 * 开始考试流程
	 */
	public void start(){
		try {
			ExamInfo examInfo = examService.start();
			QuestionInfo questionInfo = examService.getQuestion(0);
			this.currentQuestionInfo=questionInfo;
			this.examInfo = examInfo;
			startTimer();
			examFrame.updateView(examInfo,questionInfo);
			menuFrame.setVisible(false);
			examFrame.setVisible(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			JOptionPane.showMessageDialog(menuFrame, e.getMessage());;
		}
	}
	/**
	 * 分数流程
	 */
	public void result(){
		try {
			int score=examService.getScore();
			JOptionPane.showMessageDialog(examFrame, "您 的总分为"+score);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(examFrame, e.getMessage());
		}
	}
	public void prev(){
		try {
			int index = currentQuestionInfo.getQuestionIndex();
			List<Integer> answers = examFrame.getUserAnswers();
			examService.saveUserAnswers(index,answers);
			index--;
			currentQuestionInfo = examService.getQuestion(index);
			examFrame.updateView(examInfo, currentQuestionInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			JOptionPane.showMessageDialog(examFrame, e.getMessage());
		}
	}
	public void next(){
		try {
			int index = currentQuestionInfo.getQuestionIndex();
			List<Integer> answers = examFrame.getUserAnswers();
			examService.saveUserAnswers(index,answers);
			index++;
			currentQuestionInfo= examService.getQuestion(index);
			examFrame.updateView(examInfo, currentQuestionInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			JOptionPane.showMessageDialog(examFrame, e.getMessage());
		}
	}
	public void send(){
		int val=JOptionPane.showConfirmDialog(examFrame, "确定要提交？");
		if(val==JOptionPane.YES_OPTION){
			gameOver();
		}
	}
	private void gameOver() {
		// TODO Auto-generated method stub
		try {
			int index = currentQuestionInfo.getQuestionIndex();
			List<Integer> answers = examFrame.getUserAnswers();
			examService.saveUserAnswers(index, answers);
			int score = examService.over();
			if(timer!=null){
				timer.cancel();
			}
			JOptionPane.showMessageDialog(examFrame, "您 的分数为"+score);
			examFrame.setVisible(false);
			menuFrame.setVisible(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(examFrame, e.getMessage());
		}
	}
	private void startTimer(){
		try {
			timer = new Timer();
			final long end = System.currentTimeMillis() + examInfo.getTimeLimit()*1000*60;
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					long show = end - System.currentTimeMillis();
					long h = show/1000/60/60;
					long m = show/1000/60%60;
					long s = show/1000%60;
					examFrame.updateTimer(h,m,s);
				}
			}, 0,1000);
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					gameOver();
					timer.cancel();
				}
			}, new Date(end));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			JOptionPane.showMessageDialog(examFrame, e.getMessage());
		}
	}
}
